//
//  MyLocalAlbumCollectionController.m
//  OGL
//
//  Created by 洋 耿 on 15/4/19.
//  Copyright (c) 2015年 ZengYuan. All rights reserved.
//

#import "MyLocalAlbumCollectionController.h"
#import "REPhotoThumbnailsCell.h"
//#import "MJPhoto.h"
#import "LocalMJPhotoBrowser.h"
#import "LocalPhoto.h"
#import "MJPhoto.h"


@interface MyLocalAlbumCollectionController() <REPhotoThumbnailsCellDelegate>

@end

@implementation MyLocalAlbumCollectionController
@synthesize thumbnailViewClass = _thumbnailViewClass;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self removeHeaderView];
}

- (void)setLocalAlbumArray:(NSMutableArray *)localAlbumArray{
    _localAlbumArray = localAlbumArray;
    
    NSMutableArray *tempArray = (NSMutableArray *)[[localAlbumArray reverseObjectEnumerator] allObjects];
    
    self.datasource = tempArray;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        if (localAlbumArray.count == 0 || localAlbumArray == nil) {
            [self addFullloadView];
            [fullloadingView showErrorMsg:noPhotoTip];
        }
        
        [self reloadData];
    });
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
//    [self.rightBtnPar setTitle:@"管理" forState:UIControlStateNormal];
    self.titleLabel.text = @"本地视频截图";
}

-(void)setTipInfo{

    noPhotoTip = @"暂无视频截图";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"REPhotoThumbnailsCell";
    REPhotoThumbnailsCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[REPhotoThumbnailsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier thumbnailViewClass:_thumbnailViewClass];
    }
    cell.deleagte=self;
    cell.indexPath = indexPath;
    REPhotoGroup *group = (REPhotoGroup *)[_ds objectAtIndex:indexPath.section];
    
    NSInteger startIndex = indexPath.row * 4;
    NSInteger endIndex   = startIndex + 4;
    if (endIndex > [group.items count])
        endIndex = [group.items count];
    
    [cell removeAllPhotos];
    for (NSInteger i = startIndex; i < endIndex; i++) {
        NSObject <REPhotoObjectProtocol> *photo = [group.items objectAtIndex:i];
        [cell addPhoto:photo];
    }
    [cell refresh];
    
    return cell;
}

-(void)didSelected:(NSInteger)photoGroupIndex thumbnail:(NSInteger)thumbIndex thumbImgView:(UIImageView*)imgview{
    NSLog(@"%ld, %ld", (long)photoGroupIndex, (long)thumbIndex);
    
    
    REPhotoGroup *group = (REPhotoGroup *)[_ds objectAtIndex:photoGroupIndex];
    
    NSMutableArray *photos = [NSMutableArray array];
    for (int i=0; i<group.items.count; i++) {
        LocalPhoto* photo = [group.items objectAtIndex:i];
        
        MJPhoto *mjphoto = [[MJPhoto alloc] init];
        mjphoto.uuid = photo.uuid;
        mjphoto.image = photo.originImage;
        mjphoto.srcImageView = imgview;    //源imgview
        mjphoto.thumbnailImage = photo.thumbnailImage;    //缩略小图
        [photos addObject:mjphoto];
    }
    
    // 2.显示相册
    LocalMJPhotoBrowser *browser = [[LocalMJPhotoBrowser alloc] init];
    browser.currentPhotoIndex = thumbIndex; // 弹出相册时显示的第一张图片是？
    browser.photos = photos; // 设置所有的图片
    [browser show];
    

    
}

@end
